module I18n
  def self.ts *arr
    j = (I18n.locale.to_s=='en' ? ' ' : '')
    arr.flatten.map{|a|a.is_a?(Class) ? a.model_name.human : I18n.t(a)}.join(j)
  end
end

class NilClass
  def label
    ''
  end

  def split *arg
    []
  end

  def format_date(type)
    ''
  end

end

class Date
  def full_with_dot
    self.strftime("%Y.%m.%d")
  end
end

class Time
  def label
    self.strftime("%Y-%m-%d %H:%M:%S")
  end

  def first_day_of_the_same_month
    Time.parse("#{self.year}-#{self.month}-01")
  end

  def format_date(type)
    case type
      when :full
        self.strftime("%Y-%m-%d %H:%M:%S")
      when :min
        self.strftime("%Y-%m-%d %H:%M")
      when :date
        self.strftime("%Y-%m-%d")
      when :date_with_dot
        self.strftime("%Y.%m.%d")
      when :mon
        self.strftime("%Y-%m")
      when :day
        self.strftime("%Y年%m月%d日")
      when :month
        self.strftime("%Y年%m月")
      when :year
        self.strftime("%Y")
      when :quarter
        [self.strftime("%Y"), "年", [["第一", "第二", "第三", "第四"][(self.month-1)/3], "季度"]].join
    end
  end

  def inspect
    label
  end

  def self.start_of_today
    new.strftime("%Y-%m-%d 00:00")
  end

  def self.end_of_today
    new.strftime("%Y-%m-%d 23:59")
  end

end

class Enumerize::Attribute
  def options_for_search
    options.map{|o| [o[0], (self.instance_variable_get(:@klass).to_s + '::' + self.name.to_s.upcase).constantize[o[1].to_sym]]}
  end
end

class Hash
  # hash = {aa: 1, bb: 2}
  # hash.to_obj
  #=> {aa: 1, bb: 2}
  # hash.aa
  #=> 1
  def to_object
    self.keys.each do |key|
      self.define_singleton_method key.to_sym do
        self[key]
      end

      self.define_singleton_method "#{key}=".to_sym do |v|
        self[key] = v
      end
    end
    self
  end
  alias to_obj to_object
end
